﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Rules;

namespace Validation
{
    public class StringMaxValidation : ValidationBase
    {
        public int MaxLenngth { get; set; }
        public StringMaxValidation(Csla.Core.IPropertyInfo PrimaryProperty, string ValidationMessage, int MaxLenght)
            : base(PrimaryProperty, ValidationMessage)
        {
            this.MaxLenngth = MaxLenght;
            InputProperties = new List<Csla.Core.IPropertyInfo> { PrimaryProperty };
        }

        protected override void Execute(RuleContext context)
        {
            string primaryValue = (string)context.InputPropertyValues[PrimaryProperty];
            if (primaryValue != "" && primaryValue.Length > MaxLenngth)
            {
                context.AddErrorResult(this.ValidationMessage);
                return;
            }            
        }
    }
}
